<?php
/**
 * @author Erling Owe <erling.owe@gmail.com>
 * @package Core
 * @subpackage Controllers
 */ 

/**
 * Handles the request and dispatches an answer.
 *
 * This class is a singleton
 */
class Core_Controllers_Front {
   
    private static $objInstance;

    private $arrURI; // TODO: Rename to $arrURISegments.


    function __construct () {
      
        session_start();

        $this->arrURI = explode( "/", substr( $_SERVER["REQUEST_URI"], 1 ) );
      
    }

    /**
     * Gets an instance of itself.
     *
     * @return Core_Controllers_FrontController
     */
    public static function getInstance () {
      
        if ( !self::$objInstance instanceof self )
            self::$objInstance = new self;
      
        return self::$objInstance;
      
    }

    private function getFirstLevelOfURI () {
        return $this->arrURI[0];
    }

    private function removeFirstLevelOfURI () {
        $this->arrURI = array_shift( $this->arrURI );
    }

    public function process () {

        switch ( $this->getFirstLevelOfURI() ) {

            default:

                $strName = $this->getFirstLevelOfURI();
                $strFilename = Core_Directories::CONTROLLERS . $strName . "Controller.php";

                if ( file_exists( $strFilename ) ) {
                    $strControllerName = $strName;
                    $strControllerFilename = $strFilename;
                    $this->removeFirstLevelOfURI();
                } else {
                    $strControllerName = "Index";
                    $strControllerFilename = Core_Directories::CONTROLLERS . "IndexController.php";
                    // TODO: Throw an error if {CONTROLLERS}/IndexController.php does not exist.
                }


                require_once $strControllerFilename;

                $strControllerClassname = $strControllerName . "Controller";

                $strActionName = ( method_exists( $strControllerClassname, $this->getFirstLevelOfURI() . "Action" ) ) ? $this->getFirstLevelOfURI() : "Index";
                // TODO: Throw an error if the method IndexAction does not exist.

                $ActionController = new $strControllerClassname ( $strControllerName, $strActionName );

                $strActionMethodName = $strActionName . "Action";
                $ActionController->$strActionMethodName();
                $ActionController->View->dispatch();

        }

//      /**
//       * @todo $_GET['Page'] is data from the user and must be treated as such.
//       */
//      $strPage = ( isset( $_GET['Page'] ) ) ? $_GET['Page'] : "Index";
//      $strPageController = $strPage . "Controller";
//
//      // NOTE: This doesn't need to be a page. It could be an event, and should
//      // then be passed to an event controller. It could also be a "Hi, I'm
//      // alive. Do your background stuff"-call. Or RSS?
//
//      // http://somedomain.com/rss/somefeed
//      // http://somedomain.com/event/someevent
//
//      require_once "../application/controllers/" . $strPageController . ".php";
//
//      $objPageController = new $strPageController();
//
//      $objPageController->dothedo();
      
    }

/* Force download.   
header("Content-Disposition: attachment; filename=" . urlencode($file));    
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");             
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.

$fp = fopen($file, "r"); 
while (!feof($fp))
{
    echo fread($fp, 65536); 
    flush(); // this is essential for large downloads
}  
fclose($fp); 
*/   
   
}

?>
